package com.zhoug.zmui;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.zhoug.common.utils.ResourceUtils;
import com.zhoug.zmui.alpha.ZMUIAlphaTextView;
import com.zhoug.zmui.utils.ZMUIUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 一个通用的空界面控件，支持显示 loading、主标题、副标题和按钮，这些控件按顺序从上到下排列，并可按需组合显示
 *在Theme中用zmui_style_ZMUIEmptyView定义全局属性
 * @Author 35574
 * @Date 2020/7/3
 * @Description
 */
public class ZMUIEmptyView extends FrameLayout {
    private static final String TAG = ">>>ZMUIEmptyView";
    private static final boolean DEBUG=false;
    private ZMUILoadView mLoadView;
    private TextView mTvTitle;
    private TextView mTvDetail;
    private ZMUIAlphaTextView mBtn;

    public ZMUIEmptyView(@NonNull Context context) {
        this(context, null, 0);
    }

    public ZMUIEmptyView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZMUIEmptyView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.zmui_empty_view, null);
        mLoadView = view.findViewById(R.id.progress);
        mTvTitle = view.findViewById(R.id.tv_title);
        mTvDetail = view.findViewById(R.id.tv_detail);
        mBtn = view.findViewById(R.id.btn);
        addView(view);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ZMUIEmptyView, R.attr.zmui_style_ZMUIEmptyView, R.style.zmui_style_ZMUIEmptyView);
        int loadViewSize = typedArray.getDimensionPixelOffset(R.styleable.ZMUIEmptyView_zmui_loadView_size, 0);
        Drawable loadViewDrawable = typedArray.getDrawable(R.styleable.ZMUIEmptyView_zmui_loadView_image);
        float titleSize = typedArray.getDimension(R.styleable.ZMUIEmptyView_zmui_emptyView_titleSize, ResourceUtils.spTopx(14));
        float detailSize = typedArray.getDimension(R.styleable.ZMUIEmptyView_zmui_emptyView_detailSize, ResourceUtils.spTopx(12));
        int titleColor = typedArray.getColor(R.styleable.ZMUIEmptyView_zmui_emptyView_titleColor, ZMUIUtils.UN_USED_COLOR);
        int detailColor = typedArray.getColor(R.styleable.ZMUIEmptyView_zmui_emptyView_detailColor, ZMUIUtils.UN_USED_COLOR);

        int btnWidth = typedArray.getLayoutDimension(R.styleable.ZMUIEmptyView_zmui_emptyView_button_width, ViewGroup.LayoutParams.WRAP_CONTENT);
        int btnHeight = typedArray.getLayoutDimension(R.styleable.ZMUIEmptyView_zmui_emptyView_button_height, ViewGroup.LayoutParams.WRAP_CONTENT);
        Drawable btnDrawable = typedArray.getDrawable(R.styleable.ZMUIEmptyView_zmui_emptyView_button_drawable);
        float btnTextSize = typedArray.getDimension(R.styleable.ZMUIEmptyView_zmui_emptyView_button_textSize, ResourceUtils.spTopx(14));
        ColorStateList btnTextColor = typedArray.getColorStateList(R.styleable.ZMUIEmptyView_zmui_emptyView_button_textColor);

        typedArray.recycle();
        //设置progress的属性
        mLoadView.setLoadViewSize(loadViewSize);
        mLoadView.setLoadViewImage(loadViewDrawable);
        //设置文本属性
        ZMUIUtils.setTextSize(mTvTitle,titleSize);
        ZMUIUtils.setTextSize(mTvDetail,detailSize);
        ZMUIUtils.setTextColor(mTvTitle,titleColor);
        ZMUIUtils.setTextColor(mTvDetail,detailColor);
        //设置button属性
        ViewGroup.LayoutParams layoutParams = mBtn.getLayoutParams();
        layoutParams.width = btnWidth;
        layoutParams.height = btnHeight;
        mBtn.setLayoutParams(layoutParams);
        if(btnDrawable!=null){
            mBtn.setBackground(btnDrawable);
        }
        ZMUIUtils.setTextSize(mBtn,btnTextSize);
//        ZMUIUtils.setTextColor(mBtn,btnTextColor);
        mBtn.setTextColor(btnTextColor);
        init();
    }


    private void init() {

    }


    /**
     * 切换 loading 的显示/隐藏，同时隐藏标题、副标题和按钮
     * @param loading
     */
    public void show(boolean loading) {
        mLoadView.setVisibility(loading ? View.VISIBLE : View.GONE);
        mTvTitle.setVisibility(GONE);
        mTvDetail.setVisibility(GONE);
        mBtn.setVisibility(GONE);
        if (getVisibility() != VISIBLE) {
            setVisibility(VISIBLE);
        }
    }

    /**
     * 显示标题和副标题(如果需要只显示其中之一可设置另一个为 null)，同时隐藏 loading 和按钮
     *
     * @param titleText  标题
     * @param detailText 副标题
     */
    public void show(String titleText, String detailText) {
        mLoadView.setVisibility(GONE);
        mBtn.setVisibility(GONE);

        if (titleText != null) {
            mTvTitle.setText(titleText);
            mTvTitle.setVisibility(VISIBLE);
        } else {
            mTvTitle.setVisibility(GONE);
        }
        if (detailText != null) {
            mTvDetail.setText(detailText);
            mTvDetail.setVisibility(VISIBLE);
        } else {
            mTvDetail.setVisibility(GONE);
        }
        if (getVisibility() != VISIBLE) {
            setVisibility(VISIBLE);
        }
    }

    /**
     * @param titleText             标题
     * @param detailText            副标题
     * @param buttonText            按钮文本,默认:重新加载
     * @param onButtonClickListener 按钮单击事件
     */
    public void show(String titleText, String detailText, String buttonText, OnClickListener onButtonClickListener) {
        mLoadView.setVisibility(GONE);
        if (titleText != null) {
            mTvTitle.setText(titleText);
            mTvTitle.setVisibility(VISIBLE);
        } else {
            mTvTitle.setVisibility(GONE);
        }
        if (detailText != null) {
            mTvDetail.setText(detailText);
            mTvDetail.setVisibility(VISIBLE);
        } else {
            mTvDetail.setVisibility(GONE);
        }
        if (buttonText == null) {
            buttonText = "重新加载";
        }
        mBtn.setText(buttonText);
        mBtn.setVisibility(VISIBLE);
        mBtn.setOnClickListener(onButtonClickListener);

        if (getVisibility() != VISIBLE) {
            setVisibility(VISIBLE);
        }
    }

    /**
     * 隐藏整个 QMUIEmptyView，并清除各个控件的内容
     */
    public void hide() {
        setVisibility(GONE);
        mBtn.setText(null);
        mBtn.setOnClickListener(null);
        mTvTitle.setText(null);
        mTvTitle.setText(null);
    }

    public TextView getTitleText() {
        return mTvTitle;
    }

    public TextView getDetailText() {
        return mTvDetail;
    }

    public TextView getButton() {
        return mBtn;
    }

    public ZMUILoadView getLoadView() {
        return mLoadView;
    }
}
